package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import bean.DadosEntradaRNA;
import util.Constantes;
import util.Util;
import java.awt.Dimension;
import java.io.File;

import language.Strings;

public class DetalhesTelaEstatisticas extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;

	private JLabel lbFluxoOriginal = null;
	private JLabel lbFluxoNorm = null;
	private JLabel lbMa = null;
	private JLabel lbMb = null;
	private JLabel lbTd = null;
	private JLabel lbAa = null;
	private JLabel lbAb = null;
	private JLabel lbNa = null;
	private JLabel lbTa = null;
	private JLabel lbTb = null;
	private JLabel lbSaidaIdeal = null;
	
	private JTextField tfMa = null;
	private JTextField tfMb = null;
	private JTextField tfAa = null;
	private JTextField tfAb = null;
	private JTextField tfTa = null;
	private JTextField tfTb = null;
	private JTextField tfTd = null;
	private JTextField tfNa = null;
	private JTextField tfSaidaIdeal = null;
	
	private JTextArea jTextAreaNorm = null;
	private JTextArea jTextAreaOriginal = null;
	private JScrollPane jScrollPaneNorm = null;
	private JScrollPane jScrollPaneOriginal = null;
	/**
	 * This is the default constructor
	 */
	public DetalhesTelaEstatisticas() {
		super();
		initialize();
		
		configuraFontesTF();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(716, 391);
		this.setContentPane(getJContentPane());
		this.setTitle(Strings.TITULO_DETALHES);
		
		this.setResizable(false);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setLocation(
						((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (this
								.getWidth() / 2)),
						((Toolkit.getDefaultToolkit().getScreenSize().height / 2) - (this
								.getHeight() / 2)));
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbFluxoNorm = new JLabel();
			lbFluxoNorm.setBounds(new Rectangle(360, 11, 191, 15));
			lbFluxoNorm.setText(Strings.TEXT_LABEL_FLUXO_NORMALIZADO + ":");
			lbFluxoOriginal = new JLabel();
			lbFluxoOriginal.setBounds(new Rectangle(17, 11, 160, 15));
			lbFluxoOriginal.setText(Strings.TEXT_LABEL_FLUXO_ORIGINAL + ":");

			lbSaidaIdeal = new JLabel();
			lbSaidaIdeal.setBounds(new Rectangle(200, 290, 105, 15));
			lbTb = new JLabel();
			lbTb.setBounds(new Rectangle(322, 260, 43, 15));
			lbTa = new JLabel();
			lbTa.setBounds(new Rectangle(322, 230, 43, 15));
			lbNa = new JLabel();
			lbNa.setBounds(new Rectangle(490, 260, 43, 15));
			lbAb = new JLabel();
			lbAb.setBounds(new Rectangle(167, 260, 43, 15));
			lbAa = new JLabel();
			lbAa.setBounds(new Rectangle(167, 230, 43, 15));
			lbTd = new JLabel();
			lbTd.setBounds(new Rectangle(490, 230, 43, 15));
			lbMb = new JLabel();
			lbMb.setBounds(new Rectangle(20, 260, 43, 15));
			lbMa = new JLabel();
			lbMa.setBounds(new Rectangle(20, 230, 43, 15));

			lbSaidaIdeal.setText(Strings.TEXT_LABEL_SAIDA_IDEAL + ":");
			lbTb.setText(Strings.TEXT_LABEL_TB + ":");
			lbTa.setText(Strings.TEXT_LABEL_TA + ":");
			lbNa.setText(Strings.TEXT_LABEL_NA + ":");
			lbAb.setText(Strings.TEXT_LABEL_AB + ":");
			lbAa.setText(Strings.TEXT_LABEL_AA + ":");
			lbTd.setText(Strings.TEXT_LABEL_TD + ":");
			lbMb.setText(Strings.TEXT_LABEL_MB + ":");
			lbMa.setText(Strings.TEXT_LABEL_MA + ":");
			
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJScrollBarNorm(), null);
			jContentPane.add(getJScrollBarOriginal(), null);
			jContentPane.add(lbMa, null);
			jContentPane.add(lbMb, null);
			jContentPane.add(lbTd, null);
			jContentPane.add(lbAa, null);
			jContentPane.add(lbAb, null);
			jContentPane.add(lbNa, null);
			jContentPane.add(lbTa, null);
			jContentPane.add(lbTb, null);
			jContentPane.add(lbSaidaIdeal, null);
			jContentPane.add(lbFluxoOriginal, null);
			jContentPane.add(lbFluxoNorm, null);
			jContentPane.add(getTfMa(), null);
			jContentPane.add(getTfMb(), null);
			jContentPane.add(getTfAa(), null);
			jContentPane.add(getTfAb(), null);
			jContentPane.add(getTfTa(), null);
			jContentPane.add(getTfTb(), null);
			jContentPane.add(getTfTd(), null);
			jContentPane.add(getTfNa(), null);
			jContentPane.add(getTfSaidaIdeal(), null);
			jContentPane.setBackground(Color.WHITE);
		}
		return jContentPane;
	}
	
	private JTextArea getJTextAreaNorm() {
		if (jTextAreaNorm == null) {
			jTextAreaNorm = new JTextArea();
			jTextAreaNorm.setBounds(new Rectangle(360, 15, 290, 147));
			jTextAreaNorm.setEditable(false);
		}
		
		return jTextAreaNorm;
	}

	private JScrollPane getJScrollBarNorm() {
		if (jScrollPaneNorm == null) {
			jScrollPaneNorm = new JScrollPane(getJTextAreaNorm());
			jScrollPaneNorm.setBounds(new Rectangle(360, 35, 320, 147));
		}
		jScrollPaneNorm.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jScrollPaneNorm.setPreferredSize(new Dimension(3, 18));
		jScrollPaneNorm.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		return jScrollPaneNorm;
	}

	private JTextArea getJTextAreaOriginal() {
		if (jTextAreaOriginal  == null) {
			jTextAreaOriginal = new JTextArea();
			jTextAreaOriginal.setBounds(new Rectangle(17, 15, 290, 147));
			jTextAreaOriginal.setEditable(false);
		}
				
		return jTextAreaOriginal;
	}

	private JScrollPane getJScrollBarOriginal() {
		if (jScrollPaneOriginal  == null) {
			jScrollPaneOriginal = new JScrollPane(getJTextAreaOriginal());
			jScrollPaneOriginal.setBounds(new Rectangle(17, 35, 320, 147));
		}
		jScrollPaneOriginal.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jScrollPaneOriginal.setPreferredSize(new Dimension(3, 18));
		jScrollPaneOriginal.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		return jScrollPaneOriginal;
	}

	private JTextField getTfMa() {
		if (tfMa == null) {
			tfMa = new JTextField();
			tfMa.setBounds(new Rectangle(60, 230, 70, 15));
			tfMa.setEditable(false);
			
		}
		return tfMa;
	}

	private JTextField getTfMb() {
		if (tfMb == null) {
			tfMb = new JTextField();
			tfMb.setBounds(new Rectangle(60, 260, 70, 15));
			tfMb.setEditable(false);
			
		}
		return tfMb;
	}

	private JTextField getTfAa() {
		if (tfAa == null) {
			tfAa = new JTextField();
			tfAa.setBounds(new Rectangle(197, 230, 90, 15));
			tfAa.setEditable(false);
			
		}
		return tfAa;
	}

	private JTextField getTfAb() {
		if (tfAb == null) {
			tfAb = new JTextField();
			tfAb.setBounds(new Rectangle(197, 260, 90, 15));
			tfAb.setEditable(false);
			
		}
		return tfAb;
	}

	private JTextField getTfTa() {
		if (tfTa == null) {
			tfTa = new JTextField();
			tfTa.setBounds(new Rectangle(362, 230, 90, 15));
			tfTa.setEditable(false);
			
		}
		return tfTa;
	}

	private JTextField getTfTb() {
		if (tfTb == null) {
			tfTb = new JTextField();
			tfTb.setBounds(new Rectangle(362, 260, 90, 15));
			tfTb.setEditable(false);
			
		}
		return tfTb;
	}

	private JTextField getTfTd() {
		if (tfTd == null) {
			tfTd = new JTextField();
			tfTd.setBounds(new Rectangle(530, 230, 90, 15));
			tfTd.setEditable(false);
			
		}
		return tfTd;
	}

	private JTextField getTfNa() {
		if (tfNa == null) {
			tfNa = new JTextField();
			tfNa.setBounds(new Rectangle(530, 260, 90, 15));
			tfNa.setEditable(false);
			
		}
		return tfNa;
	}

	private JTextField getTfSaidaIdeal() {
		if (tfSaidaIdeal == null) {
			tfSaidaIdeal = new JTextField();
			tfSaidaIdeal.setBounds(new Rectangle(300, 290, 50, 15));
			tfSaidaIdeal.setEditable(false);
			
		}
		return tfSaidaIdeal;
	}
	
	private void configuraFontesTF(){
		Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 11);
		getTfMa().setFont(font);
		getTfMb().setFont(font);
		getTfAa().setFont(font);
		getTfAb().setFont(font);
		getTfTa().setFont(font);
		getTfTb().setFont(font);
		getTfTd().setFont(font);
		getTfNa().setFont(font);
		getTfSaidaIdeal().setFont(font);
	}
	
	private void listarFluxoNorm(String pathArquivo){
		File file = new File(Constantes.DIRETORIO_PERFIL_ESTATISTICA_FLUXO_NORMALIZADO, 
				pathArquivo + Constantes.EXTENSAO_TXT);
		
		StringBuffer buffer = UtilitariosGUI.LISTAR_FLUXO(file);
		
		if(buffer != null){
			getJTextAreaNorm().setText(buffer.toString());
		} else{
			getJTextAreaNorm().setText("");
		}
	}
	
	private void listarFluxoOriginal(String pathArquivo){
		File file = new File(Constantes.DIRETORIO_PERFIL_ESTATISTICA_FLUXO_ORIGINAL, 
				pathArquivo + Constantes.EXTENSAO_TXT);
		
		StringBuffer buffer = UtilitariosGUI.LISTAR_FLUXO(file);
		
		if(buffer != null){			
			getJTextAreaOriginal().setText(buffer.toString());
		} else{
			getJTextAreaOriginal().setText("");
		}
	}
	
	private void mostraDadosEntradaRNA(String pathArquivo){
		File file = new File(Constantes.DIRETORIO_PERFIL_ESTATISTICA_CSV, pathArquivo + Constantes.EXTENSAO_CSV);
		DadosEntradaRNA input = Util.getDadosEntradaRNA(file);
		
		if(input == null){
			return;
		}
		
		getTfMa().setText(String.valueOf(input.getMa()));
		getTfMb().setText(String.valueOf(input.getMb()));
		getTfAa().setText(String.valueOf(input.getAa()));
		getTfAb().setText(String.valueOf(input.getAb()));
		getTfTa().setText(String.valueOf(input.getTa()));
		getTfTb().setText(String.valueOf(input.getTb()));
		getTfTd().setText(String.valueOf(input.getTd()));
		getTfNa().setText(String.valueOf(input.getNa()));
		getTfSaidaIdeal().setText(String.valueOf(input.getSaidaIdeal()));
	}
		
	public void executa(String nome){
		this.setTitle(nome);
		
		mostraDadosEntradaRNA(nome);
		listarFluxoNorm(nome);
		listarFluxoOriginal(nome);
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
